 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=208 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:59 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Try alternatives to ResourceBundle
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Try alternatives to ResourceBundle</div>

<div class='main-body'>
 
<br>There are some rather disturbing problems with 
<a href="http://java.sun.com/javase/6/docs/api/java/util/ResourceBundle.html">ResourceBundle</a> :
<ul>
<li>if the translations are stored in a database, there is no way for an application
to create a new localization without code changes (that is, without creating a new 
database-backed <tt>ResourceBundle</tt> class for each new <tt>Locale</tt>).
<li>prior to JDK 6, there is no way of refreshing the underlying data at runtime. 
</ul>

<P>
<em>These are serious defects.</em>


<P>In addition, there are many nuisances associated with using <tt>ResourceBundle</tt>s and their 
associated properties files : 
<ul>

<li>the encoding used in a properties file is ISO-8859-1, not UTF-8. If the developer is using a 
language which does not use ISO-8859-1, then the <tt>native2ascii</tt> tool must be used to process the files.
(Rather rude for an internationalization mechanism, is it not?)

<li>natural text keys versus coder keys : from the point of the caller, using a natural text lookup key 
has the advantage that it increases legibility. However, such a lookup key appears as 
<tt>blah=blah</tt> in a properties file. Such duplication makes most programmers justifiably nervous. (If a 
database is used, a <tt>ResultSet</tt> having the same <tt>blah=blah</tt> structure can be 
easily created, but without any actual data repeated in the underlying tables.)

<li>in a properties file, there is no easy way to find duplicates. In general, there 
is <em>no easy way to perform any sort of query</em> on the data (as there is in a database).

<li>mailing raw properties files to translators does not seem desirable, but 
building a front end tool for such files is not easy - so it never gets done

<li>if an item has more than one line, then the line continuation character "\" must be appended to each line

<li>apostrophe characters are a problem : the 
<a href="http://java.sun.com/javase/6/docs/api/java/text/MessageFormat.html">MessageFormat</a> 
class treats apostrophes as special characters. If an apostrophe appears 
in a pattern passed to a <tt>MessageFormat</tt>, it must be escaped, or else 
a nice little <tt>RuntimeException</tt> is thrown. If you send properties files to translators, 
the probabilty of errors related to apostrophes is <em>very</em> high. 
If a database is used instead of properties files, however, then 
it is possible to build a front end tool which can do the necessary validations. 
(Building such a tool is especially attractive when it can be used with multiple projects.)

<li>use of module names : the <tt>ResourceBundle</tt> mechanism allows the caller to specify a module name, 
which is mapped into a corresponding class name or properties file name. It is possible 
to seriously question this design. 
In general, <em>a method should not know the identity of its caller</em>. 
Now, translation is just another 
feature. Why should should this particular feature care about which <em>other</em> feature is calling it? 
What compelling reason is there for breaking this basic rule of program design? 
The only thing a translation needs is the base text and the target <tt>Locale</tt>. That is all. 
If there is any context which is important to the 
translation, then that context is either a linguistic context, or a user interface context.
It is not, strictly speaking, attached to a program <em>feature</em>. 
If you are still not convinced, ask yourself this question : does 
a human translator care <em>in any way</em> if text comes from feature A or feature B? <em>In any way?</em> 
If you ponder this question closely, the answer will likely be "no". 
</ul>

<P>In summary, it seems that <tt>ResourceBundle</tt> was intended 
originally for desktop applications, and that its design is not robust 
enough for easy use in the context of a web application backed by a database.

<P>Fortunately, text translation is not a difficult task, and 
creating an alternative to <tt>ResourceBundle</tt> is actually fairly easy. 
For an example, please see the 
<a href="http://www.web4j.com/web4j/javadoc/hirondelle/web4j/ui/translate/package-summary.html">hirondelle.web4j.ui.translate</a> 
package of the <a href="TopicAction5f31-2.html">WEB4J</a> tool. It uses a technique called 
"last second localization".
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicActionb69d-2.html?Id=73'>Internationalization</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='208'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=208 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:00 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
